package com.zzyl.service.impl;

import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public int addBed(BedDto bedDto) {
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedDto, bed);
        // 设置默认值
        bed.setBedStatus(0);
       /* bed.setCreateTime(LocalDateTime.now());
        bed.setUpdateTime(LocalDateTime.now());
        // TODO 填充创建人和修改人

        bed.setCreateBy(1L);
        bed.setUpdateBy(1L);
*/
        try {
            return bedMapper.insertBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public int deleteBed(Long id) {
        return bedMapper.deleteBed(id);
    }

    @Override
    public int updateBed(BedDto bedDto) {
        Bed bed = new Bed();
        log.info(bedDto.toString());
        BeanUtils.copyProperties(bedDto, bed);
        //更新修改时间
        bed.setUpdateTime(LocalDateTime.now());
        //TODO 填充修改人
        bed.setUpdateBy(1L);
        try {
            return bedMapper.updateBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_UPDATE_FAIL);
        }
    }

    @Override
    public BedVo getBedById(Long id) {
        Bed bed = bedMapper.getBedById(id);
        BedVo bedvo = new BedVo();
        BeanUtils.copyProperties(bed, bedvo);
        return bedvo;
    }

}

